controller="";
var timerIntervalle = 0;
partieCommencee = false;

function demarrer(){
	if (controller==""){
		partieCommencee = true;
		controller= new Controller();	
	}
	else{
	}
}
	
function Controller(){

	this.currentlyPressedKeys = {};
	this.timerInterval = 10;
	
	//COUNTERS
	this.timerCounter = 0;
	this.readyCounter = 0;
	this.readyCounterMax = 200;
	this.endRoundCounter = 0;
	this.endRoundCounterMax = 200;
	
	this.initEvents();

	this.game=new Game(this);
	this.view=new View(this);
	
	this.timer = setInterval(worldStep,this.timerInterval);
}

Controller.prototype.initEvents = function() {

	//KEY EVENTS
	document.onkeydown = keyDown;
	document.onkeyup = keyUp;
	document.onmousedown = mouseDown;
	document.onmousemove = mouseMove;
	document.onmouseup = mouseUp;
	document.ondrop= drop;
	document.ondragover=dragOver;
}

	//TIMER//
worldStep =  function() {
	if (controller.game.rankList[controller.game.turnIndex].ready || controller.game.rankList[controller.game.turnIndex].pass) {
		if (controller.readyCounter < controller.readyCounterMax) {
			controller.readyCounter++;
		}
		else {
			controller.readyCounter = 0;
			if (controller.endRoundCounter < controller.endRoundCounterMax) {
				controller.endRoundCounter++;
			}
			else
				controller.endRoundCounter = 0;
				controller.game.nextTurn();
		}
	}
	if (controller.game.rankList[controller.game.turnIndex].done) {
		controller.game.nextTurn();
	}

	controller.view.redraw();
		
	controller.timerCounter++;
}

//KEYEVENTS//

drag = function(event) {
	
}

dragOver = function(event) {
	
}

drop = function(event) {
	
}

keyDown = function(event) {
	//this.currentlyPressedKeys[event.keyCode] = true;
}

keyUp = function(event) {
	//this.currentlyPressedKeys[event.keyCode] = false;
}

mouseDown = function(event) {
	var x = getMousePositionX(event);
	var y = getMousePositionY(event);
	//console.log("posX" + x);
	//console.log("posY" + y);
	controller.game.clickEvent(x,y);
}

mouseMove = function(event) {
	var x = getMousePositionX(event);
	var y = getMousePositionY(event);
	//console.log("posX" + x);
	//console.log("posY" + y);
	controller.game.mouseMoved(x,y);
}

mouseUp = function(event) {
	var x = getMousePositionX(event);
	var y = getMousePositionY(event);
	//console.log("posX" + x);
	//console.log("posY" + y);
	controller.game.unclickEvent(x,y);
}

function getMousePositionX(e) {
	var IE = document.all?true:false;
	var tempX = 0;
	
	if (IE) { 
		tempX = event.clientX + document.body.scrollLeft;
	}
	else {  
		tempX = e.pageX;
	}  
	var pos = findPos(document.getElementById("canvas"));
	return tempX-pos.x;
}

function getMousePositionY(e) {
	var IE = document.all?true:false;
	var tempY = 0;
	
	if (IE) { 
		tempY = event.clientY + document.body.scrollLeft;
	}
	else {  
		tempY = e.pageY;
	}  
	
	var pos = findPos(document.getElementById("canvas"));
	return tempY-pos.y;
}

function findPos(obj) {
    var curleft = 0, curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
        return { x: curleft, y: curtop };
    }
    return undefined;
}

function getKeyCode(e) {
	if (navigator.appName == "Microsoft Internet Explorer") {
			return event.keyCode;
	}	   
	return e.which;
}; 